package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-09-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements IEmployeeService {

    @Resource
    public EmployeeMapper employeeMapper;
    @Autowired
    IEmployeeService iEmployeeService;



    @Override
    public List<Employee> queryEmployeeListByName(Integer page, Integer size, String name) {

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<Employee>();
        QueryWrapper<Employee> wrapper = employeeQueryWrapper.eq("name", name);

        List<Employee> employeeList = iEmployeeService.list(wrapper);
//
//        Page<Employee> employeePage = new Page<>(page, size);
//        Page<Employee> employeePage1 = employeeMapper.selectPage(employeePage, employeeList);
//
////        List<Employee> list = employeeMapper.selectEmpListByName(name);

        return employeeList;

    }


    /**
     * 分页方法
     * @param num
     * @param size
     * @param queryWrapper
     * @return
     */
    @Override
    public List<Employee> queryEmpPage(Integer num, Integer size, QueryWrapper<Employee> queryWrapper) {

        IPage<Employee> employeeIPage=new Page<>(num,size);
//          //employeeMapper.selectPage(employeeIPage,employeeList);

        employeeIPage=employeeMapper.selectPage(employeeIPage,queryWrapper);

        List<Employee> list = employeeIPage.getRecords();

        return list;
    }

    @Override
    public String getNewId() {

        Integer newId= (Integer)(employeeMapper.getMaxId()+1);
        return newId.toString();
    }

    @Override
    public List<String> selectAllNations() {
        List<String> list= employeeMapper.selectAllNations();
        return list;
    }

    @Override
    public List<Employee> queryAllEmp(Integer page, Integer size,String name) {




//        List<Employee> list = iEmployeeService.list();
        if (name==null||"".equals(name)){
//            List<Employee> list = employeeMapper.queryAllEmp();
            List<Employee> list = queryEmpPage(page, size, null);
//            List<Employee> list1 = iEmployeeService.list();
            return list;
        }else{
//            List<Employee> list =employeeMapper.queryEmpByName(name);
            QueryWrapper<Employee> wrapper = new QueryWrapper<>();
            QueryWrapper<Employee> wrapper1 = wrapper.like("name", name);
            List<Employee> list = queryEmpPage(page, size, wrapper1);
//            List<Employee> list1 = iEmployeeService.list(wrapper1);
            return list;
        }

//    @Override
//    public List<Employee> queryAllEmp(Integer page, Integer size,String name) {
//
//
//
////        List<Employee> list = iEmployeeService.list();
//        if (name==null||"".equals(name)){
//            List<Employee> list = employeeMapper.queryAllEmp();
//            return list;
//        }else{
//            List<Employee> list =employeeMapper.queryEmpByName(name);
//            return list;
//        }





    }
}
